var reg = {
		
		//初始化
		init: function(notNull,pwdDiff){
			//创建用户
			$(".btn.btn-submit").click(function(){
				$("label.error").text("");
				$(".err-msg").hide();
				var email = $(".emailText").val();
				var password = $(".password").val();
				var password2 = $(".password2").val();
				var imageCode = $(".imageCode").val();
				var checkbox = $("input:checkbox:checked").size();
				var check = true;
				if(""==email){
					$(".emailText").parent("div").find("label").text(notNull);
					check = false;
				}
				if(""==password){
					$(".password").parent("div").find("label").text(notNull);
					check = false;
				}
				if(""==password2){
					$(".password2").parent("div").find("label").text(notNull);
					check = false;
				}
				if(""==imageCode){
					$(".imageCode").parent("div").find("label").text(notNull);
					check = false;
				}
				if(checkbox==0){
					$("input:checkbox").parent("label").parent("div").find(".error").text(notNull);
					check = false;
				}
				if(password!=password2){
					$(".password2").parent("div").find("label").text(pwdDiff);
				}
				
				if(!check){
					return false;
				}
				//提交请求
				$.post("/register/mail",{
					email:email,
					password:password,
					imageCode:imageCode
				},function(data){
					if(data.code=="200"){
						post("/register/1",{
							email:email,
							password:password,
							imageCode:imageCode
						});
					}else{
						$(".err-msg").text(data.msg)
						$(".err-msg").show();
					}
				},"json");
				
				
			});
			$("#image").click(function(){
				var $that = $(this);
				$that.attr("src","/valicode?t="+new Date().getTime());
			});
		},
		reg3:{
			timers:	180,
			tipId: "",
			init: function(){
				//手机号修改事件
				$(".regMobile").keyup(function(){
					var regMobile = $(this).val();
					var reg = new RegExp("^1[345789]\\d{9}$");
		    		if (reg.test(regMobile)) {
		    			$(".sendSms").removeClass("disabled");
		    		}
				});
				
				$("#image").click(function(){
					var $that = $(this);
					$that.attr("src","/valicode?t="+new Date().getTime());
				});
				//发送短息
				$(".sendSms").click(function(){
					
					var $that = $(this);
					if($that.hasClass("disabled")){
						return false;
					}
					$(".err-msg").hide();
					
					$(".imageCode").parent("div").removeClass("has-error");
					var callCode = $(".callShow").attr("callCode");
					var imageCode = $(".imageCode").val();
					if(imageCode==""){
						$(".imageCode").parent("div").addClass("has-error");
						return false;
					}
					$.post("/sendSms",{
						mobile:$(".regMobile").val(),
						callCode:callCode,
						imageCode:imageCode
					},function(data){
						if(data.code=="200"){
							$that.addClass("disabled");
							var text = $that.text();
							reg.reg3.timers=180;
							reg.reg3.tipId=window.setInterval("reg.reg3.timer_('"+text+"')",1000);
						}else{
							$(".err-msg").text(data.msg);
							$(".err-msg").show();
							$(".sendSms").removeClass("disabled");
						}
					},"json")
					
				});
				
				//提交
				$(".btn.btn-submit").click(function(){
					$(".err-msg").hide();
					var mobile = $(".regMobile").val();
					var smsCode = $(".smsCode").val();
					var callCode = $(".callShow").attr("callCode");
					if(mobile==""){
						$(".regMobile").parent("div").addClass("has-error");
						return false;
					}
					if(smsCode==""){
						$(".smsCode").parent("div").addClass("has-error");
						return false;
					}
					var $that = $(this);
					$.post("/register/mobile",{
						userId:$("#userId").val(),
						mobile:mobile,
						smsCode:smsCode,
						callCode:callCode
					},function(data){
						if(data.code=="200"){
							window.location.href="/register/3";
						}else{
							$(".err-msg").text(data.msg);
							$(".err-msg").show();
						}
					},"json")
					
				});
				
				$(".callCode").click(function(){
					//查询数据
					$.post("/findDictionary",{
						code:"CALL_CODE",
						text:""
					},function(data){
						$(".callItem").remove();
						var html = "";
						$.each(data,function(index,e){
							html+="<li  class=\"ng-scope callItem\" callCode=\""+e.value+"\" ><a href=\"#\" class=\"ng-binding\">"+e.text+"("+e.value+")</a></li>";
						});
						$(".callHtml").append(html);
					},"json");
				});
				
				$("body").on("click",".callItem",function(){
					var $that = $(this);
					$(".callShow").text($that.find("a").text());
					$(".callShow").attr("callCode",$that.attr("callCode"));
				});
				
				$(".select-search-content input").keyup(function(){
					//查询数据
					var $that = $(this);
					$.post("/findDictionary",{
						code:"CALL_CODE",
						text:$that.val()
					},function(data){
						$(".callItem").remove();
						var html = "";
						$.each(data,function(index,e){
							html+="<li  class=\"ng-scope callItem\" callCode=\""+e.value+"\" ><a href=\"#\" class=\"ng-binding\">"+e.text+"("+e.value+")</a></li>";
						});
						$(".callHtml").append(html);
					},"json");
				});
				
				
			},
			timer_: function(text){
				if(reg.reg3.timers>=0){
					$(".sendSms").text(reg.reg3.timers+"S");
					reg.reg3.timers--;
				}else{
					window.clearInterval(reg.reg3.tipId);
					$(".sendSms").text(text);
					$(".sendSms").removeClass("disabled");
				}
			}
		},
		reg2:	{
			timers:	90,
			tipId: "",
			init:function(email,password,imageCode,sendText){
				reg.reg2.tipId=window.setInterval("reg.reg2.timer_('"+sendText+"')",1000);
				
				$(".btn.btn-empty").click(function(){
					var $that = $(this);
					if($that.hasClass("disabled")){
						return false;
					}
					$that.addClass("disabled");
					//提交请求
					$.post("/register/mail",{
						email:email,
						password:password,
						imageCode:imageCode
					},function(data){
						if(data.code=="200"){
							reg.reg2.timers=90;
							reg.reg2.tipId=window.setInterval("reg.reg2.timer_('"+sendText+"')",1000);
						}else if(data.code=="1002"){
							//图片验证码失效
							window.location.href="/register";
						}else{
							$(".err-msg").text(data.msg)
							$(".err-msg").show();
							$(".btn.btn-empty").removeClass("disabled");
						}
					},"json");
				});
				
			},
			timer_: function(text){
				if(reg.reg2.timers>=0){
					$(".btn.btn-empty").text(reg.reg2.timers+"S");
					reg.reg2.timers--;
				}else{
					window.clearInterval(reg.reg2.tipId);
					$(".btn.btn-empty").text(text);
					$(".btn.btn-empty").removeClass("disabled");
				}
			}
		}
}